import java.lang.*;
import java.util.*;

public class TypeA extends Thread 
{
	private static Semaphore G3, G1, G2, G5;
	private static Storage St;
	private static DataCollection Data;
	private static System currentTime;
	private static Random randomA = new Random(1);
	
	private static void MoveToG3()
	{
		double g3Time = 500-100+2*100*randomA.nextFloat(); /* 0.5+-0.1 hour, define 1hour = 1000ms */
		try
		{
			sleep((long)g3Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	private static void MoveToG1()
	{
		double g1Time = 600-100+2*100*randomA.nextFloat(); /* 0.6+-0.1 hour */
		try
		{
			sleep((long)g1Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	private static void MoveToG2()
	{
		double g2Time = 850-100+2*100*randomA.nextFloat(); /* 0.85+-0.1 hour */
		try
		{
			sleep((long)g2Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	private static void MoveToG5()
	{
		double g5Time = 500-100+2*100*randomA.nextFloat(); /* 0.5+-0.1 hour */
		try
		{
			sleep((long)g5Time);			
		}
		catch (InterruptedException e)
		{
			System.out.println(e);
		}
	}

	
	public TypeA(Semaphore group3, Semaphore group1, Semaphore group2, Semaphore group5, Storage myStorage, DataCollection d)
	{
		G3 = group3;
		G1 = group1;
		G2 = group2;
		G5 = group5;
		St = myStorage;
		Data = d;
		// Thread can be set as a daemon thread in the constructor
        setDaemon(true);
        

	}
	
	public void run()
	{
			
		long tBeg, tG30, tG31, tG10, tG11, tG20, tG21, tG50, tG51, tStO0, tStO1, tEnd;
		
		tBeg = currentTime.currentTimeMillis(); 
		
		//move to G3
		tG30 = currentTime.currentTimeMillis(); 
		if(G3.getValue()<=0)
		{
			Data.getG3WQ();
		}	
		G3.take();
		tG31 = currentTime.currentTimeMillis();
		Data.getG3WT(tG31-tG30); //get G3 waiting time  		
  		
  		MoveToG3();
  	  		
  		tStO0 = currentTime.currentTimeMillis();  
  		if(St.getValue()>8)
		{
			Data.getStOQ();
		}			
  		St.release(); 
	  	tStO1 = currentTime.currentTimeMillis();
	  	Data.getStOT(tStO1-tStO0);//get storage overflow time     	
	  	
  		G3.release();  		
  		  
  		
  		//move to G1
		tG10 = currentTime.currentTimeMillis(); 
		if(G1.getValue()<=0)
		{
			Data.getG1WQ();
		}	   			
		G1.take();
		tG11 = currentTime.currentTimeMillis();
		Data.getG1WT(tG11-tG10); //get G1 waiting time
		 		      		
  		MoveToG1();
  		G1.release();  		
  	
  		
  		//move to G2
		tG20 = currentTime.currentTimeMillis(); 
		if(G2.getValue()<=0)
		{
			Data.getG2WQ();
		}	
		G2.take();    			
		tG21 = currentTime.currentTimeMillis();
		Data.getG2WT(tG21-tG20);//get G2 waiting time	
  		     		
  		MoveToG2();      		
  		G2.release(); 
  		
  		//move to G5
		tG50 = currentTime.currentTimeMillis();
		if(G5.getValue()<=0)
		{
			Data.getG5WQ();
		}	
		G5.take();    			
		tG51 = currentTime.currentTimeMillis();
		Data.getG5WT(tG51-tG50);//get G5 waiting time	
  		     		
  		MoveToG5();
  		G5.release(); 
  		
  		//type1 residence time and throughput
  		tEnd = currentTime.currentTimeMillis();  		
  		Data.getAR(tEnd - tBeg); 
      	
      	//System.out.println("T1 Running");
    }
}